-- UIDungeonBlacksmith
-- Created by chengb Mar/11/2015
-- 地牢工坊界面1（装备选择）

require "game/ui/form/blacksmith/UIDungeonBlacksmith2"
require "game/ui/form/equips/UIEquipTooltip"
require "game/ui/form/equips/UIEquips"

-- UIDungeonBlacksmith 继承自Layer
UIDungeonBlacksmith = class("UIDungeonBlacksmith", function()
    return cc.Layer:create();
end);

function UIDungeonBlacksmith.create(pos)
    return UIDungeonBlacksmith.new(pos);
end

-- 内部函数声明
local resize;

-- 构造函数
function UIDungeonBlacksmith:ctor(pos)
    self:setName("UIDungeonBlacksmith");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/blacksmith/DungeonBlacksmith.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 记录格子的位置
    self.pos = pos;
    self.gridData = DungeonM.getGridByPos(pos);
    local elemId = self.gridData.element;
    local elemData = DungeonM.queryElement(elemId, "dbase");
    self.upgradeStyle = elemData.upgrade_style;

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    resize(node);
end

-- 适配
function resize(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 注册事件处理回调函数
function UIDungeonBlacksmith:registerEventCallback()
    -- 关注工坊放入装备的事件
    EventMgr.register("UIDungeonBlacksmith", event.BLACKSMITH_INPUT_EQUIP, function(args)
        self:whenInputEquip(args);
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIDungeonBlacksmith 界面析构清理");
            EventMgr.removeAll("UIDungeonBlacksmith");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
                UIDungeonMgr:getCurLevel():showTop();
            end
        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
                UIDungeonMgr:getCurLevel():hideTop();
            end
        end
    end);
end

-- 注册点击事件
function UIDungeonBlacksmith:registerTouchEvent()
    local node = self.node;

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIDungeonBlacksmith");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_confirm");
            -- 判断是否放入装备
            if not self.equipId then
                alert(getLocStr("please_input_equip"));
                return;
            end

            -- 打开工坊操作界面
            local uiDungeonBlacksmith2 = UIDungeonBlacksmith2.create(self.equipId, self.isEquiped, self.pos);
            UIMgr.getCurrentScene():addForm(uiDungeonBlacksmith2);
        end
    end
    btnOk:addTouchEventListener(onOkClick);

    -- 注册装备点击格点击事件
    local equipGrid = findChildByName(node, "CT/equip_node/frame");
    local function onEquipClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 打开装备选择界面
            -- 传入参数：通过工坊打开
            local uiEquip = UIEquips.create(EQUIP_OPEN_TYPE_BLACKSMITH, {["upgradeStyle"] = self.upgradeStyle, });
            UIMgr.getCurrentScene():addForm(uiEquip);
        end
    end
    equipGrid:addTouchEventListener(onEquipClick);
end

-- 重绘
function UIDungeonBlacksmith:redraw()
    local CT = findChildByName(self.node, "CT");
    local BT = findChildByName(self.node, "BT");

    -- 显示标题
    local title = "";
    local name = BuildingBonusM.queryBuildingView(self.gridData.class, "title");
    if type(name) == "string" and name ~= "" then
        -- 如果配置了标题，直接使用
        title = name;
    else
        -- 使用默认标题
        title = getLocStr("blacksmith_text");
    end

    local titleLabel = findChildByName(CT, "title_node/title");
    TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    titleLabel:setString(title);

    -- 显示建筑图标
    local blacksmithImg = findChildByName(CT, "title_node/building_icon");
    local icon = self.gridData.icon or "blacksmith";
    blacksmithImg:loadTexture(getLevelItemIconPath(icon));

    -- 显示功能描述
    local tipLabel = findChildByName(CT, "tip");
    TextStyleM.setTextStyle(tipLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    tipLabel:setString(getLocStr("blacksmith_tip"));

    -- 显示装备框
    local equipImg = findChildByName(self.node, "CT/equip_node/equip_icon");
    self.equipImg = equipImg;
    self:redrawEquip();

    -- 显示确定按钮
    local btnOk = findChildByName(CT, "btn_ok");
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("btn_text_ok"));

    -- 显示返回按钮
    local btnBack = findChildByName(BT, "btn_back");
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 重绘装备格
function UIDungeonBlacksmith:redrawEquip()
     local frame = findChildByName(self.node, "CT/equip_node/frame");

    if not self.equipId then
        checkAddBubbleStatus(self.equipImg);
        return;
    end

    frame:removeAllChildren();
    self.equipImg:setVisible(true);
    local icon = ItemM.query(self.equipId, "icon");
    self.equipImg:loadTexture(getItemIconPath(icon));
end

-- 工坊放入装备的回调
function UIDungeonBlacksmith:whenInputEquip(args)
    local equipId = args.classId;
    if not equipId then
        return;
    end

    -- 记录装备id
    self.equipId = equipId;
    self.isEquiped = args.isEquiped;

    -- 重绘装备格
    self:redrawEquip();
end


